import { Injectable } from '@angular/core';
import { Router } from "@angular/router";
import { Http, Headers } from "@angular/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/of";
import "rxjs/add/operator/do";
import "rxjs/add/operator/delay";
import { AppConfig } from "../config";
import { ApplicationService } from "../service/application.service";

/**
 * 用户验证服务
 * 
 * @export
 * @class AuthService
 */
@Injectable()
export class AuthService {
  private readonly headers = new Headers({
    'Content-Type': 'application/json',
  });
  private readonly param = "auth/login";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  /**
   * 验证错误
   * 
   * @private
   * @type {*}
   * @memberof AuthService
   */
  private _authError: any;
  /**
   * 验证结果
   * 
   * @private
   * @type {boolean}
   * @memberof AuthService
   */
  private _authReason: boolean;
  /**
   * 登录进度
   * 
   * @private
   * @type {boolean}
   * @memberof AuthService
   */
  private _loginProgress: boolean;
  /**
   * 重定向Url
   * 
   * @private
   * @type {string}
   * @memberof AuthService
   */
  private _redirectUrl: string = "";

  /**
   * 验证错误
   * 
   * @readonly
   * @type {*}
   * @memberOf AuthService
   */
  get authError(): any {
    return this._authError;
  }
  /**
   * 验证错误
   * 
   * @readonly
   * @type {*}
   * @memberOf AuthService
   */
  set authError(value: any) {
    this._authError = value;
  }

  /**
  * 验证结果
  * true：已通过，false：未通过
  * @readonly
  * @type {boolean}
  * @memberOf AuthService
  */
  get authReason(): boolean {
    return this._authReason;
  }
  /**
   * 登录进度
   * true：请求结束，false：请求正在进行，未结束
   * @readonly
   * @type {boolean}
   * @memberOf AuthService
   */
  get loginProgress(): boolean {
    return this._loginProgress;
  }

  /**
   * 读取或设置重定向Url
   * 
   * @readonly
   * @type {string}
   * @memberOf AuthService
   */
  get redirectUrl(): string {
    return this._redirectUrl;
  }
  /**
   * 设置重定向Url
   * 
   * 
   * @memberOf AuthService
   */
  set redirectUrl(url: string) {
    this._redirectUrl = url;
  }
  constructor(
    private router: Router,
    private http: Http,
    private applicationService: ApplicationService ) {
  }

  
  //添加了callbackURL
  login(userName: string, passWord: string,callbackURL:string): Promise<any> { 
    this._authReason = false;
    this._loginProgress = true;
    // const url = `http://192.168.15.150:8585/webapi/v1/auth/login`;
    return this.http.post(this.url, JSON.stringify({ userName: userName, passWord: passWord, callbackURL: callbackURL }), { headers: this.headers })
      .toPromise()
      .then(res => {
        try {
          let result = res.json();
          switch (result.code) {
            case "200":
              this._authReason = true;
              this._loginProgress = false;
              return result.data;
            case "-1":
              this._authReason = false;
              throw new Error(result.message);
          }
        }
        catch (error) {
          this._loginProgress = false;
          throw error;
        }
      })
      .catch(error => this.handleError);
  }


  // /**
  //  * 登出
  //  * 
  //  * 
  //  * @memberOf AuthService
  //  */
  // logout(): void {
  //   this.router.navigate(["/login"]);

  //   this._loginProgress = false;
  //   //清除本地存储
  //   window.localStorage.clear();
  //   // window.sessionStorage.clear();
  // }
  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}



// WEBPACK FOOTER //
// ./src/app/service/auth.service.ts